import React from 'react';
import router from 'umi/router';
import moment from 'moment';

import { connect } from 'dva';

import { Button,Form, Input, Select,DatePicker } from 'antd';


const FormItem = Form.Item;

const { Option } = Select;

const linePath = '/AccountConfig/ResPartner';


@connect(({
  resPartner, accountAccount, resCountryState
}) => ({
  resPartner, accountAccount, resCountryState

}))
class FormCreate extends React.Component {
  state = {
  }

  id = 0

  async componentDidMount() {

    // 获取多对一字段的下拉列表框选项所需要的数据



    const { location: {query: { id } } } = this.props;
    this.id = Number(id)
    if (this.id) {
      const { dispatch } = this.props;
      await dispatch({
        type: 'resPartner/view',
        payload:  { id: this.id, }
      });

    }

    const { dispatch } = this.props;
    await dispatch({
      type: 'resCountryState/search',
      payload: {domain:[], fields:{} },
    });


    await dispatch({
      type: 'accountAccount/search',
      payload: {domain:[], fields:{} },
    });



  }

  handleSubmit = e => {
    const { dispatch, form } = this.props;
    e.preventDefault();

    const create_or_write = async (vals) => {
      if(this.id){
        await dispatch({
          type: 'resPartner/write',
          payload : {
            id: this.id,  vals,
          },
        });

        router.push(`${linePath}?id=${this.id}`);
      }
      else{
        await dispatch({
          type: 'resPartner/create',
          payload : {
            vals,
            fields:{}
          },
        });

        const { resPartner:{record} } = this.props;

        if (record.id){
          router.push(`${linePath}?id=${record.id}`);
        }
      }

    }


    form.validateFieldsAndScroll( async (err, values) => {

      if (!err) {
        const { date } = values;

        const vals = { ...values,
          date: date.format('YYYY-MM-DD'),
        };

        const {property_account_payable_id, property_account_receivable_id } = values

        if ( ! property_account_payable_id ) {
          delete vals.property_account_payable_id
        }

        if ( ! property_account_receivable_id ) {
          delete vals.property_account_receivable_id
        }



        await create_or_write(vals)

      }
    });
  };


  render() {
    const { form: { getFieldDecorator }  } = this.props;
    const { resPartner: { record } } = this.props;
    const { resCountryState: { recordsList: countryStateList } } = this.props;
    const { accountAccount: { recordsList: accountList } } = this.props;

    return (
      <div>
          <Form onSubmit={this.handleSubmit}>
            <FormItem style={{ marginTop: 32 }}>
              <Button type="primary" htmlType="submit" >提交
              </Button>
            </FormItem>

            <FormItem label="名称">
              {getFieldDecorator('name', {
                rules: [{ required: true }],
                initialValue: record.name || '',
              })(
                <Input />
              )}
            </FormItem>

            <FormItem label="代码">
              {getFieldDecorator('ref', {
                rules: [{ required: true }],
                initialValue: record.ref || '',
              })(
                <Input />
              )}
            </FormItem>

            <FormItem label="付款科目">
              {getFieldDecorator('property_account_payable_id', {
                //rules: [{ required: true }],
                initialValue: record.property_account_payable_id ? record.property_account_payable_id.id : 0
              })(
                <Select placeholder="" >
                  <Option value={0} >未选择</Option>
                  {accountList.map(item => {
                    return (
                      <Option value={item.id} key="id">
                        {`${item.code} ${item.name}`}
                      </Option>
                    );
                  })}

                </Select>

              )}
            </FormItem>

            <FormItem label="收款科目">
              {getFieldDecorator('property_account_receivable_id', {
                //rules: [{ required: true }],
                initialValue: record.property_account_receivable_id ? record.property_account_receivable_id.id : 0
              })(
                <Select placeholder="" >
                  <Option value={0} >未选择</Option>
                  {accountList.map(item => {
                    return (
                      <Option value={item.id} key="id">
                        {`${item.code} ${item.name}`}
                      </Option>
                    );
                  })}

                </Select>

              )}
            </FormItem>


            <FormItem label="日期">
              {getFieldDecorator('date', {
                rules: [{ required: true }],
                initialValue: record.date ? moment(record.date, 'YYYY-MM-DD') : moment()
               // initialValue: moment().format('YYYY-MM-DD')
               // initialValue: moment(record.date, 'YYYY-MM-DD'),
              })(<DatePicker />)}
            </FormItem>

            <FormItem label="省">
              {getFieldDecorator('state_id', {
                rules: [{ required: true }],
                initialValue: record.state_id ? record.state_id.id : 0
              })(
                <Select placeholder="" >
                  <Option value={0} >未选择</Option>
                  {countryStateList.map(item => {
                    return (
                      <Option value={item.id} key="id">
                        {item.name}
                      </Option>
                    );
                  })}

                </Select>

              )}
            </FormItem>

          </Form>


      </div>

    );
  }
}


export default Form.create()(FormCreate);


